<?php
/**
 * Created by PhpStorm.
 * User: alice
 * Date: 2015/12/28
 * Time: 14:46
 */

class Involved_ForestModel extends BaseModel
{
    protected $table = 'forest';

    /**************   APP接口  ****************/

    /**
     * 林场列表
     * @return array
     * @author alice
     * @date 2016-1-23
     */
    public function GetForestList()
    {
        #全部林场列表
        $forest= DB::table($this->table)
                        ->select('forest_id','country','forest_name','show_pic1','type')
                        ->get();
        #区分海外林场
        $labels    = [ 'foreign_forest' => [], 'country_forest' => []];
        $structure = [ 0 => 'country_forest', 1 =>'foreign_forest'];
        foreach($forest as $f){
            $type = $structure[$f->type];
            $labels[$type][] = [
                'forest_id'     => $f->forest_id,
                'forest_name'   => ($f->type === 1) ? $f->country : $f->forest_name,
                'show_pic1'     => $f->show_pic1
            ];
        }


        return $labels;

    }

    /**
     * 林场详情
     * @param $forest_id
     * @return array
     * @throws Exception
     * @author alice
     * @date 2016-1-23
     */
    public function GetForestById($forest_id)
    {
        $forest =  DB::table($this->table)
                ->select('forest_id','forest_name','country','product_cate','introduce','video_id','show_pic1','show_pic2','show_pic3')
                ->where(['forest_id'=>$forest_id])
                ->first();
        if(empty($forest))
            throw new Exception('林场不存在 ', ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST);


        $data = [
            'forest_id'         => $forest->forest_id,
            'forest_name'       => $forest->forest_name,
            'country'           => $forest->country,
            'product_cate'      => $forest->product_cate,
            'introduce'         => str_replace('/r/n','',strip_tags($forest->introduce)),
            'video_id'          => $forest->video_id,
            'img'               =>[
                                    $forest->show_pic1,
                                    $forest->show_pic2,
                                    $forest->show_pic3,
                                  ],
            'video_url'         => HostEnum::HOST_NAME.'/Api/video_play?type_id='.HostEnum::VIDEO_TYPE_FOREST.'&video_id='.$forest->video_id
//            'video_url'         => "http://media.mutouw.com/3079ce3b792214a35c579d5446090f79.mp4"
        ];
        return $data;
    }





    /**************   PC端  ****************/

    /**
     * 林场列表
     * @author alice
     * @date 2016-1-19
     * @return
     */
    public function ShowForestList()
    {
        return DB::table($this->table)->paginate(8);

    }

    /**
     * 林场详情
     * @param $id
     * @author alice
     * @date 2016-1-19
     * @return mixed
     */
    public function ShowForestInfo($id)
    {
        return DB::table($this->table)->where('forest_id',$id)->first();

    }
    /**
     * 林场
     * @return mixed
     * @author lizhongpeng
     * data 2016-03-30
     */
    public function GetForest()
    {
        return DB::table($this->table)
            ->select('forest_id','forest_name', 'country', 'product_cate','show_pic1','video_id')->get();
    }

    /**
     * 编辑
     */
    public function GetForestInfo($id)
    {
        return DB::table($this->table)->where('forest_id',$id)->first();
    }

    /**
     * 获取视频id
     */
    public function GetVideo($id)
    {
        return DB::table($this->table)->select('video_id')->where('forest_id',$id)->first();
    }


}